import Vue from 'vue'
import Vuex from 'Vuex'
Vue.use(Vuex)
const key = 'name'
const name = localStorage.getItem('name')
const uname = localStorage.getItem('uname')
// 创建vuex实例对象

const store = new Vuex.Store({

  state() {
    return {
      name: null,
      uname: null
    }
  },

  getters: {
    getStorage: function (state) {
      if (!state.name) {
        let wintoken = localStorage.getItem(key)
        state.name = wintoken
      }
      return state.name
    },
    getuname: function (start) {
      if (!start.uname) {
        let uname = localStorage.getItem(null)
        start.uname = uname
      }
      return start.uname
    }
  },
  mutations: {
    $_setStorage(state, value) {
      state.name = value.token
      window.localStorage.setItem(key, value.token)
    },
    $_remobeStorage(state) {
      console.log("移除:",state)
      state.name = null
      window.localStorage.removeItem(key)
    },

    // 修改或删除名称
    setuname(state, value) {
      state.uname = value
      window.localStorage.setItem('uname', value)
    },
    remouname(state) {
      state.uname = null
      window.localStorage.removeItem('uname')
    }
  }
})
export default store
